cd "C:\Users\sertsios\Dropbox\LPSU replication package RFS\Replication\Log files\Main Tables"
clear
log using Table2.txt, text replace

***Table 2

clear
cd "C:\Users\sertsios\Dropbox\LPSU replication package RFS\Data"
use LPSU_MainSample_Sep2024, replace

cd "C:\Users\sertsios\Dropbox\LPSU replication package RFS\Tables"

**Keep pre-attempt variables
keep if year_ar<0

**Getting averages over the periods
foreach i of varlist wroa assets_MM sales_MM sales_empl_w sales_at_w wleverage cash_ratio_w sic_q_w sales_gr_w pyramid_pre_ipo subs internl   {
bysort id: egen mean_`i'=mean(`i')
}

*

keep mean* lag30 id pos_returns ipo
duplicates drop

label var mean_wroa "OROA"
label var mean_assets_MM "Assets (MM)"
label var mean_sales_MM "Sales (MM)"
label var mean_sales_empl_w "Sales(MM)/Empl"
label var mean_sales_at_w "Sales/Assets"
label var mean_wleverage "Leverage"
label var mean_cash_ratio_w "Cash/Assets"
label var mean_sic_q_w "Ind. q"
label var mean_sales_gr_w "Sales growth"
label var mean_pyramid_pre_ipo "Pyramid"
label var mean_subs "Subsidiary"
label var mean_internl "International"
label var ipo "Completed IPO"
*label var mean_div "Multiple Ind."
*label var mean_nr_deals "Acquisitions"
*label var mean_age_w "Age"
*label var mean_num_countries_w "# Countries"
*label var mean_num_sic3_w "# Industries"
*label var mean_num_subs_w "# Subs"

**Getting median returns
egen med_ret=median(lag30)
sum lag30, d
gen high_returns=0
replace high_returns=1 if lag30>med_ret


**Panel A: Split by the treatment
*********************************
local i =1
foreach l of varlist mean_wroa mean_assets_MM mean_sales_MM mean_sales_empl_w mean_sales_at_w mean_wleverage mean_cash_ratio_w ///
  mean_sic_q_w mean_sales_gr_w mean_pyramid_pre_ipo mean_subs mean_internl ///
 lag30 pos_returns {
qui: reg `l'  ipo
         matrix b_b = get(_b)'
		 matrix b_b`i'= J(1,3,.)
		 matrix b_b`i'[1,3] = _b[ipo]

         matrix b_s = get(_b)', vecdiag(cholesky(diag(vecdiag(get(VCE)))))'
		 matrix b_se= J(1,2,.)
		 matrix b_se[1,1] = b_s[1,1]
 		 matrix b_se[1,2] = b_s[1,2]	
	

local bc = rowsof(b_se)
matrix stars= J(`bc',2,0)
forvalues k = 1/`bc' {
            matrix stars[`k',2] = (abs(b_se[`k',1]/b_se[`k',2]) > invttail(`e(df_r)',0.1/2)) + (abs(b_se[`k',1]/b_se[`k',2]) > invttail(`e(df_r)',0.05/2)) + (abs(b_se[`k',1]/b_se[`k',2]) > invttail(`e(df_r)',0.01/2))
		 }	
	
		
		matrix stars_2`i'= J(1,3,.)
		matrix stars_2`i'[1,1]= 0
		matrix stars_2`i'[1,2]= 0
		matrix stars_2`i'[1,3]= stars[1,2]


		qui: tabstat `l' , s( mean) c(s) by(ipo) save
		matrix A = r(Stat1)
		matrix B = r(Stat2)
		matrix b_b`i'[1,1] = A[1,1]			
		matrix b_b`i'[1,2] = B[1,1]	
*drop mat A B
mat rownames b_b`i' = `l'		 	 	
local i = `i'+1
}
*
matrix define out3 = (b_b1 \ b_b2 \ b_b3 \ b_b4 \ b_b5 \ b_b6\ b_b7 \ b_b8\ b_b9 \ b_b10 \ b_b11 \ b_b12 \ b_b13 \ b_b14 )

matrix define out4 = (stars_21 \ stars_22 \ stars_23 \ stars_24 \ stars_25 \ stars_26 \ stars_27\ stars_28\ stars_29 \ stars_210 \ stars_211 \ stars_212 \ stars_213 \ stars_214) 


frmttable using IPO_returnsx.doc, statmat(out3)  sdec(3,3,3,3,3,3,3) annotate(out4)  asymbol(*,**,***) varlabels replace  ctitle("Variable (pre IPO)", "Withdrawn" , "Completed IPO", "Diff.")

tabstat ipo , s(N) c(s) by(ipo) save

matrix C= [r(Stat1) ,r(Stat2) ,.]

frmttable using IPO_withdrawn.doc, statmat(C) sdec(0,0,0,0,0,0,0) rtitle("# of firms") append



*Panel B: Split by the instrument
**********************************
set matsize 10000

local i =1
foreach l of varlist mean_wroa mean_assets_MM mean_sales_MM mean_sales_empl_w mean_sales_at_w mean_wleverage mean_cash_ratio_w ///
  mean_sic_q_w mean_sales_gr_w mean_pyramid_pre_ipo mean_subs mean_internl ///
 lag30 pos_returns ipo {
qui: reg `l'  high_returns
         matrix b_b = get(_b)'
		 matrix b_b`i'= J(1,3,.)
		 matrix b_b`i'[1,3] = _b[high_returns]

         matrix b_s = get(_b)', vecdiag(cholesky(diag(vecdiag(get(VCE)))))'
		 matrix b_se= J(1,2,.)
		 matrix b_se[1,1] = b_s[1,1]
 		 matrix b_se[1,2] = b_s[1,2]	
	

local bc = rowsof(b_se)
matrix stars= J(`bc',2,0)
forvalues k = 1/`bc' {
            matrix stars[`k',2] = (abs(b_se[`k',1]/b_se[`k',2]) > invttail(`e(df_r)',0.1/2)) + (abs(b_se[`k',1]/b_se[`k',2]) > invttail(`e(df_r)',0.05/2)) + (abs(b_se[`k',1]/b_se[`k',2]) > invttail(`e(df_r)',0.01/2))
		 }	
	
		
		matrix stars_2`i'= J(1,3,.)
		matrix stars_2`i'[1,1]= 0
		matrix stars_2`i'[1,2]= 0
		matrix stars_2`i'[1,3]= stars[1,2]


		qui: tabstat `l' , s( mean) c(s) by(high_returns) save
		matrix A = r(Stat1)
		matrix B = r(Stat2)
		matrix b_b`i'[1,1] = A[1,1]			
		matrix b_b`i'[1,2] = B[1,1]	
*drop mat A B
mat rownames b_b`i' = `l'		 	 	
local i = `i'+1
}
*
matrix define out = (b_b1 \ b_b2 \ b_b3 \ b_b4 \ b_b5 \ b_b6\ b_b7 \ b_b8\ b_b9 \ b_b10 \ b_b11 \ b_b12 \ b_b13 \ b_b14 \ b_b15 )

matrix define out2 = (stars_21 \ stars_22 \ stars_23 \ stars_24 \ stars_25 \ stars_26 \ stars_27\ stars_28\ stars_29 \ stars_210 \ stars_211 \ stars_212 \ stars_213 \ stars_214 \ stars_215) 


frmttable using IPO_returns.doc, statmat(out)  sdec(3,3,3,3,3,3,3) annotate(out2)  asymbol(*,**,***) varlabels replace  ctitle("Variable (pre IPO)", "Low ret." , "High ret", "Diff.")

tabstat high_returns , s(N) c(s) by(high_returns) save

matrix C= [r(Stat1) ,r(Stat2) ,.]

frmttable using IPO_returns2.doc, statmat(C) sdec(0,0,0,0,0,0,0) rtitle("# of firms") append



cd "C:\Users\sertsios\Dropbox\LPSU replication package RFS\Replication\Log files\Main Tables"
log close
